package com.examsystem.dao;


import com.examsystem.pojo.Paper;
import com.examsystem.pojo.User;
import com.examsystem.pojo.vo.StuTeacherMsg;
import com.examsystem.pojo.vo.TeacherMsg;
import com.examsystem.pojo.vo.UserMsg;
import org.apache.ibatis.annotations.*;
import org.springframework.security.access.method.P;

import java.util.List;
/**
 * @author fire'ge
 */
@Mapper
public interface UserMapper {

    /*
    学生端
     */

    /**
     * 添加一个User
     *
     * @param user 传入user类
     * @return int
     */
    int addUser(@Param("user") User user);
    /**根据id删除一个User
     * @param id 传入id
     * @return int
     */
    int deleteUserById(int id);

    /**
     * 更新User
     *
     * @param user 传入user类
     * @return int
     */
    int updateUser(User user);

    /**
     * 根据id查询,返回一个User
     *
     * @param id 传入id
     * @return int
     */
    User queryUserById(int id);

    /**
     * 根据id查询,返回一个User
     *
     * @param phoneNum 传入电话号码
     * @return int
     */
    User queryUserByUsername(@Param("userPhoneNum") String phoneNum);

    /**
     * 根据id查询,返回一个User
     *
     * @param phoneNum 传入电话号码
     * @param pwd      密码
     * @return int
     */
    User queryUserByPhoneAndPwd(@Param("userPhoneNum") String phoneNum, @Param("userPassword") String pwd);

    /**
     * 查询全部User,返回list集合
     *
     * @return List<User>
     */
    List<User> queryAllUser();

    /**根据用户的id查询userId
     * @param userId 传入的userId
     * @return 返回用户基本信息
     */
    UserMsg queryUserMsgByUserId(int userId);

    /** 修改照片url
     * @param userId 用户的id
     * @param photoUrl 照片url
     * @return 返回修改成功的条数
     */
    int updateImgByUserId(@Param("userId") int userId,@Param("photoUrl") String photoUrl);


    /** 查询该学生用户的科任老师
     * @param userId 用户的id
     * @return
     */
    List<StuTeacherMsg> queryStudentsTeacherByUserId(@Param("userId") int userId);

    /*
        教师端
     */


    /**
     * 查询用户【主要用于登录】
     *
     * @param userPhoneNum 用户手机号/账号
     * @param userPassword 用户密码
     * @return user对象
     */
    @Select("select *from user where user_identity = 2 and user_phoneNum= #{userPhoneNum} and user_password = #{userPassword} ")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_phoneNum", property = "userPhoneNum"),
            @Result(column = "user_password", property = "userPassword"),
            @Result(column = "user_identity", property = "userIdentity"),
            @Result(column = "user_class", property = "userClass")
    }
    )
    User findUserByPhonePwd(@Param("userPhoneNum") String userPhoneNum,@Param("userPassword") String userPassword);

    /**
     * 注册教师账号
     *
     * @param userPhoneNum 教师手机号
     * @param userPassword 密码
     * @param classId 班级id
     * @return user对象
     */
    @Insert("insert into user values (null,#{userPhoneNum},#{userPassword},'2',#{userClass})")
    User insertUser(@Param("userPhoneNum") String userPhoneNum,@Param("userPassword") String userPassword,@Param("classId") int classId);

    /**
     * 给教师添加学科信息
     * @param teacherId  教师id
     * @param subjectName  学科名
     * @return  【1：成功  0：失败】
     */
    @Insert("insert into subject values (0,#{subjectName},#{teacherId})")
    int addTeacherSubject(@Param("teacherId") int teacherId,@Param("subjectName") String subjectName);

    /**
     * 获得班级的id
     *
     * @param className 班级名称
     * @return int班级id
     */
    @Select("select class_id from exam.class where class_name = #{className}")
    int getClassId(String className);

    /**
     * 教师出卷【手动/自动（random）出卷：首先先创建了试卷，然后在多次执行试题与试卷的插入】
     *
     * @param paper paper对象
     * @return paper对象
     */
    @Insert("insert into exam.paper values (0,#{paper.getPaperTeacher()},#{paper.getPaperDateTime},#{paper.getPaperName})")
    Paper insertPaper(Paper paper);

    /**
     * 试卷与试题之间的关系
     *
     * @param questionId 试题id
     * @param paperId    试卷id
     * @return int 是否插入插入成功
     */
    @Insert("insert into exam.paper_question values (null,0,#{questionId},#{paperId})")
    int insertPq(@Param("questionId") int questionId,@Param("paperId") int paperId);


    /**
     * 获取教师信息
     * @param teacherId  教师id
     * @return 教师信息
     */
    @Select("select * from user where user_id = #{teacherId}")
    @Results({
            @Result(column = "user_id",property = "userId"),
            @Result(column = "user_phoneNum",property = "userPhoneNum"),
            @Result(column = "user_password",property = "userPassword"),
            @Result(column = "user_identity",property = "userIdentity"),
            @Result(column = "user_name",property = "userName"),
            @Result(column = "photo",property = "photoUrl"),
            @Result(column = "gender",property = "gender")
    })
    User getTeacherMsg(int teacherId);


    /**
     * 修改用户头像
     * @param userId  用户id
     * @param imgPath  图片路径
     * @return  【1：成功  0：失败】
     */
    @Update("update user set photo = #{imgPath} where user_id = #{userId}")
    int updateUserImg(@Param("userId") int userId,@Param("imgPath") String imgPath);

    /**
     * 修改教师账号密码
     * @param teacherId  教师id
     * @param password  密码
     * @return  【1：成功  0：失败】
     */
    @Update("update user set user_password = #{password} where user_id = #{teacherId}")
    int updateTeacherPassword(@Param("teacherId") int teacherId,@Param("password") String password);

    /**
     * 获取已批改的试卷
     * @param userId  用户id
     * @return  paper对象
     */
    @Select("select *from paper p,user_paper up where p.paperId = up.up_paper and up.up_user = #{userId} and p.paper_status = 1}")
    Paper getPaper1(int userId);


    /**
     * 获取未批改的试卷
     * @param userId  用户id
     * @return  paper对象
     */
    @Select("select *from paper p,user_paper up where p.paperId = up.up_paper and up.up_user = #{userId} and p.paper_status = 2}")
    Paper getPaper2(int userId);
}
